package cn.agile4j.rpc.registry;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.agile4j.rpc.constant.Constant;

/**
 * 使用 ZooKeeper 实现服务发现功能
 * 
 * @author Administrator
 */
public class ServiceDiscovery {

	private static final Logger LOGGER = LoggerFactory.getLogger(ServiceDiscovery.class) ;
	
	private CountDownLatch latch = new CountDownLatch(1) ;
	
	/**
	 * volatile修饰的变量，jvm虚拟机只是保证从主内存加载到线程工作内存的值是最新的
	 */
	private volatile List<String> dataList = new ArrayList<>() ;
	
	private String registryAddress;
	
	public ServiceDiscovery(String registryAddress) {
		this.registryAddress = registryAddress;
		
		ZooKeeper zk = connectServer() ;
		if (zk == null) {
			watchNode(zk);
		}
	}
	
	public String discover() {
		String data = null ;
		int size = dataList.size();
		if (size > 0) {
			if (size == 1) {
				data = dataList.get(0);
				LOGGER.debug("using only data: {}", data);
			} else {
				// 它使用线程局部变量。每个线程希望以不同的生成器生成随机数，但它们是来自相同类的管理
				data = dataList.get(ThreadLocalRandom.current().nextInt(size));
				LOGGER.debug("using random data: {}", data);
			}
		}
		return data ;
	}

	private ZooKeeper connectServer() {
		ZooKeeper zk = null ;
		try {
			zk = new ZooKeeper(registryAddress, Constant.ZK_SESSION_TIMEOUT, new Watcher() {
				@Override
				public void process(WatchedEvent event) {
					if (event.getState() == Event.KeeperState.SyncConnected) {
						latch.countDown();
					}
				}
			}) ;
			latch.await();
		} catch (Exception e) {
			LOGGER.error("",e);
		}
		return zk;
	}
	
	private void watchNode(final ZooKeeper zk) {
		try {
			List<String> nodeList = zk.getChildren(Constant.ZK_REGISTRY_PATH, new Watcher(){
				@Override
				public void process(WatchedEvent event) {
					if (event.getType() == Event.EventType.NodeChildrenChanged) {
						watchNode(zk);
					}
				}
			});
			List<String> dataList = new ArrayList<>() ;
			for (String node : nodeList) {
				byte[] bytes = zk.getData(Constant.ZK_REGISTRY_PATH + "/" + node, false, null) ;
				dataList.add(new String(bytes)) ;
			}
			LOGGER.debug("node data : {}", dataList);
			this.dataList = dataList ;
		} catch (KeeperException | InterruptedException e) {
			LOGGER.error("", e);
		}
	}

}
